package onetotwenty;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import org.junit.Test;

public class ProblemFour {
	private boolean isPalindrom(int item) {
		int t = 0;
		int t2 = item;

		while (item > 0) {
			t += item % 10;
			item /= 10;
			t *= 10;
		}

		return t2 == (t / 10);
	}

	
	
	@Test
	public void problem4() {
		assertThat(answer(), is(906609));
	}



	private int answer() {
		for (int i = 998001; i >= 10000; i--) {
			if (isPalindrom(i)) {
				for (int j = 100; j <= 999; j++) {
					if (i % j == 0 && (i / j) < 1000) {
						return i;
					}
				}
			}
		}
		
		return 1;
	}

	@Test
	public void testPalindrom() {
		assertThat(isPalindrom(1001), is(true));
		assertThat(isPalindrom(1002), is(false));
		assertThat(isPalindrom(1), is(true));
		assertThat(isPalindrom(1000001), is(true));
	}
}
